package com.qst.crop.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qst.crop.dao.KnowledgeDao;
import com.qst.crop.dao.UserDao;
import com.qst.crop.entity.Knowledge;
import com.qst.crop.entity.Order;
import com.qst.crop.service.KnowledgeService;
import lombok.extern.slf4j.Slf4j;
import com.qst.crop.service.KnowledgeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class KnowledgeServiceImpl implements KnowledgeService {

    @Autowired
    private KnowledgeDao knowledgeDao;

    @Autowired
    private UserDao userDao;
    /**
     * @Description : 分页查询
     *  * @param pageNum
     * @Return : com.qst.crop.common.Result<com.github.pagehelper.PageInfo>
     */
    @Override
    public PageInfo<Knowledge> findAll(Integer pageNum) {
        //PageHelper.startPage方法会将分页信息放入ThreadLocal中，但是 PageHelper 方法调用后紧跟查询方法，就是安全的，因为查询方法后清除了Threadlocal里的值。
        PageHelper.startPage(pageNum,30);
        List<Knowledge> knowledges =  knowledgeDao.findAll();
        PageInfo<Knowledge> knowledgePageInfo = new PageInfo<>(knowledges);
        //调用dao进行查询
        return knowledgePageInfo;
    }

    /**
     * @Description : 根据id查询知识
     *  * @param id
     * @Return : com.qst.crop.common.Result<com.qst.crop.entity.Knowledge>
     */
    @Override
    public Knowledge findById(Integer id) {
        return knowledgeDao.findById(id);
    }

    /**
     * @Description : 添加农业知识
     *  * @param knowledge
     * @Return : com.qst.crop.common.Result
     */
    @Override
    public void save(Knowledge knowledge) {
        knowledgeDao.insert(knowledge);
    }

    /**
     * @Description : 修改农业知识
     *  * @param knowledge
     * @Return : com.qst.crop.common.Result
     */
    @Override
    public void update(Knowledge knowledge) {
        log.info("updating");
        knowledgeDao.update(knowledge);
    }

    /**
     * @Description : 删除知识
     *  * @param id
     * @Return : com.qst.crop.common.Result
     */
    @Override
    public void deleteById(Integer id) {
        knowledgeDao.deleteById(id);
    }

    /**
     * @Description : 根据条件查询知识
     *  * @param condition
     * @param pageNum
     * @Return : com.qst.crop.common.Result<com.github.pagehelper.PageInfo>
     */
    @Override
    public PageInfo<Knowledge> findByCondition(String condition, int pageNum) {
        //PageHelper.startPage方法会将分页信息放入ThreadLocal中，但是 PageHelper 方法调用后紧跟查询方法，就是安全的，因为查询方法后清除了Threadlocal里的值。
        PageHelper.startPage(pageNum,30);
        List<Knowledge> knowledges =  knowledgeDao.findByCondition(condition);
        PageInfo<Knowledge> knowledgePageInfo = new PageInfo<>(knowledges);
        //调用dao进行查询
        return knowledgePageInfo;
    }

    @Override
    public List<Knowledge> getByUserName(String currentUserName) {
        return knowledgeDao.getByUserName(currentUserName);
    }

    @Override
    public List<String> getAvatar(Integer id){
        return userDao.getAvatar(id);
    }

}
